#! /bin/bash
JOBNUM=4
NPWD=`realpath .`


export ARCH=arm  
export CROSS_COMPILE=$NPWD/../../gcc/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-

if [ $1_x == cp_x ];then
	echo $PWD
	cp $NPWD/rootfs/build/linux-custom/arch/arm/boot/zImage ~/tftp
	cp $NPWD/rootfs/build/linux-custom/arch/arm/boot/dts/sun8i-h3-orangepi-one.dtb ~/tftp
elif [ $1_x == rm_x ];then	
	echo $PWD
	rm -f $NPWD/rootfs/build/uboot-v2020.07/.stamp_{target,staging,images}_installed
	rm -f $NPWD/rootfs/build/linux-custom/.stamp_{target,staging,images}_installed
elif [ $1_x == env_x ];then	
	echo $PWD
	$NPWD/../../src/buildroot/host/bin/mkimage -C none -A arm -T script -d $NPWD/boot.cmd $NPWD/boot.scr
	cp $NPWD/boot.scr $NPWD/rootfs/images/boot.scr
elif [ $1_x == uboot_x ];then	
	cd $NPWD/rootfs/build/uboot-v2020.07
	echo $PWD
	if [ $2_x == _x ];then
		make all -j$JOBNUM
	else
		make $2 -j$JOBNUM
	fi
	rm -f .stamp_{target,staging,images}_installed
elif [ $1_x == linux_x ];then	
	cd $NPWD/rootfs/build/linux-custom
	echo $PWD
	if [ $2_x == dtb_x ];then
		make sun8i-h3-orangepi-one.dtb -j$JOBNUM
	elif [ $2_x == myinstall_x ];then
		echo "my modules install to selfroot INSTALL_MOD_PATH=$NPWD/../self/rootfs"
		make modules_install INSTALL_MOD_PATH=$NPWD/../self/rootfs  -j$JOBNUM
	#elif [ $2_x == modules_x ];then
	#	make modules $3 -j$JOBNUM
	elif [ $2_x == _x ];then
		make zImage -j$JOBNUM
	else
		make $2 -j$JOBNUM
	fi
#	rm -f .stamp_{kconfig_fixup_done,configured,built}
#	rm -f .stamp_{kconfig_fixup_done,configured,built}
	rm -f .stamp_{target,staging,images}_installed
else
	cd $NPWD/../../src/buildroot
	echo $PWD
	mkdir -p $NPWD/rootfs
	make O=$NPWD/rootfs SCRDIR=$NPWD $1
fi

